package com.windystory.wslibrary.ui.webview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * webview封装
 */
public class WebView extends com.tencent.smtt.sdk.WebView {

    private Context context;

    public WebView(Context context) {
        super(context);
        this.context = context;
        initWebView();
    }

    public WebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initWebView();
    }

    public WebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initWebView();
    }


    private void initWebView() {
        new WebViewSettingsInitializer().createWebView(this);
    }


    /**
     * webviewClient
     */
    public class XWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String url) {
            webView.loadUrl(url);
            return true;
        }
    }



    /**
     * @function 对传入的webView进行各种settings，返回setting好的webView
     * Created by 尤晟 on 2017-07-30.
     */

    public class WebViewSettingsInitializer {

        @SuppressLint("SetJavaScriptEnabled")
        public WebView createWebView(final WebView webView) {
            //初始化WebSettings
            final WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            final String ua = settings.getUserAgentString();
            settings.setUserAgentString(ua + "Latte");
            //隐藏缩放控件
            settings.setBuiltInZoomControls(false);
            settings.setDisplayZoomControls(false);
            //禁止缩放
            settings.setSupportZoom(false);
            //文件权限
            settings.setAllowFileAccess(true);
            settings.setAllowFileAccessFromFileURLs(true);
            settings.setAllowUniversalAccessFromFileURLs(true);
            settings.setAllowContentAccess(true);
            //缓存相关
            settings.setAppCacheEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setDatabaseEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);

            return webView;
        }
    }
}



